#include "StdAfx.h"
#include "GetWeiboToken.h"
#include "ControlEx.h"

CGetWeiboToken::CGetWeiboToken(void)
{
}

CGetWeiboToken::~CGetWeiboToken(void)
{
}


LPCTSTR CGetWeiboToken::GetWindowClassName() const
{
	return _T("WeatherFrame"); 
}

UINT CGetWeiboToken::GetClassStyle() const 
{ 
	return CS_DBLCLKS; 
}

void CGetWeiboToken::OnFinalMessage(HWND hWnd)
{	
	delete this;
}

LRESULT CGetWeiboToken::OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	if( wParam == SC_CLOSE ) 
	{
		OnClose(uMsg, wParam, lParam, bHandled);
		return 0;
	}

	BOOL bZoomed = ::IsZoomed(*this);
	LRESULT lRes = CWindowWnd::HandleMessage(uMsg, wParam, lParam);
	if( ::IsZoomed(*this) != bZoomed ) 
	{

	}
	return lRes;
}

LRESULT CGetWeiboToken::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	LRESULT lRes = 0;
	BOOL bHandled = TRUE;
	switch( uMsg ) 
	{
	case WM_CREATE:        
		lRes = OnCreate(uMsg, wParam, lParam, bHandled); 
		break;
	case WM_CLOSE:        
		lRes = OnClose(uMsg, wParam, lParam, bHandled); 
		break;
	case WM_DESTROY:       
		lRes = OnDestroy(uMsg, wParam, lParam, bHandled); 
		break;	
	case WM_NCACTIVATE:   
		lRes = OnNcActivate(uMsg, wParam, lParam, bHandled);
		break;
	case WM_NCCALCSIZE:    
		lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled); 
		break;
	case WM_NCPAINT:      
		lRes = OnNcPaint(uMsg, wParam, lParam, bHandled);
		break;
	case WM_NCHITTEST:     
		lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled); 
		break;
	case WM_SIZE:       
		lRes = OnSize(uMsg, wParam, lParam, bHandled);
		break;
	case WM_SYSCOMMAND:  
		lRes = OnSysCommand(uMsg, wParam, lParam, bHandled);
		break;
	default:
		bHandled = FALSE;
	}
	if( bHandled ) 
		return lRes;
	if( m_pm.MessageHandler(uMsg, wParam, lParam, lRes) )
		return lRes;
	return CWindowWnd::HandleMessage(uMsg, wParam, lParam);	
}

LRESULT CGetWeiboToken::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
	styleValue &= ~WS_CAPTION;
	::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);

	m_pm.Init(m_hWnd);
	CDialogBuilder builder;
	CDialogBuilderCallbackEx cb;
	CControlUI* pRoot = builder.Create(_T("WeiboGetToken.xml"), (UINT)0,  &cb, &m_pm);
	ASSERT(pRoot && "Failed to parse XML");
	m_pm.AttachDialog(pRoot);
	m_pm.AddNotifier(this);	
	Init();
	return 0;
}

LRESULT CGetWeiboToken::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	bHandled = FALSE;
	return 0;
}

LRESULT CGetWeiboToken::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{	

	bHandled = FALSE;
	return 0;
}

LRESULT CGetWeiboToken::OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	if( ::IsIconic(*this) )
		bHandled = FALSE;
	return (wParam == 0) ? TRUE : FALSE;
}

LRESULT CGetWeiboToken::OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	return 0;
}

LRESULT CGetWeiboToken::OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	return 0;
}

LRESULT CGetWeiboToken::OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	POINT pt; pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam);
	::ScreenToClient(*this, &pt);

	RECT rcClient;
	::GetClientRect(*this, &rcClient);

	RECT rcCaption = m_pm.GetCaptionRect();
	if( pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \
		&& pt.y >= rcCaption.top && pt.y < rcCaption.bottom )
	{
		CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(pt));
		if( pControl && _tcscmp(pControl->GetClass(), _T("ButtonUI")) != 0 && 
			_tcscmp(pControl->GetClass(), _T("TextUI")) != 0 &&
			_tcscmp(pControl->GetClass(), _T("LabelUI")) != 0 &&
			_tcscmp(pControl->GetClass(), _T("EditUI")) != 0 &&
			_tcscmp(pControl->GetClass(), _T("ComboUI")) != 0 )
			return HTCAPTION;
	}

	return HTCLIENT;
}

LRESULT CGetWeiboToken::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{	
	SIZE szRoundCorner = m_pm.GetRoundCorner();
	if(wParam == SIZE_MAXIMIZED) 
	{		
		return   0; 
	}

	if( !::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0) ) 
	{
		CDuiRect rcWnd;
		::GetWindowRect(this->GetHWND(), &rcWnd);
		rcWnd.Offset(-rcWnd.left, -rcWnd.top);
		rcWnd.right++; rcWnd.bottom++;
		HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy);
		::SetWindowRgn(*this, hRgn, TRUE);
		::DeleteObject(hRgn);
	}

	bHandled = FALSE;
	return 0;
}

void CGetWeiboToken::OnExit(TNotifyUI& msg)
{
	Close();
}


void CGetWeiboToken::Init()
{
	CActiveXUI* pActiveXUI = static_cast<CActiveXUI*>(m_pm.FindControl(_T("ie")));
	if( pActiveXUI ) 
	{
		IWebBrowser2* pWebBrowser = NULL;
		pActiveXUI->GetControl(IID_IWebBrowser2, (void**)&pWebBrowser);
		if( pWebBrowser != NULL )
		{
			pWebBrowser->Navigate(L"https://api.weibo.com/oauth2/authorize?client_id=2532053590&redirect_uri=http://weibo.com/q734819992&response_type=code",NULL,NULL,NULL,NULL);  
			pWebBrowser->Release();
		}
	}
}

void CGetWeiboToken::OnPrepare(TNotifyUI& msg)
{

}

void CGetWeiboToken::Notify(TNotifyUI& msg)
{
	if (_tcsicmp(msg.sType, _T("windowinit")) == 0)
	{
		OnPrepare(msg);
	}
	else if (_tcsicmp(msg.sType, _T("killfocus")) == 0)
	{
	}
	else if (_tcsicmp(msg.sType, _T("click")) == 0)
	{
		if (_tcsicmp(msg.pSender->GetName(), _T("btn_Close")) == 0 )
		{
			OnExit(msg);
		}		
	}		
	
}